package game.boss;

import game.bullet.Bullet_Green;
import game.bullet.Bullet_Red;
import game.comp.Collection;
import game.comp.Explosion;
import game.enemy.Enemy;
import game.player.Player;
import game.tool.FileTool;
import game.tool.SoundEngine;
import game.ui.Canvas;
import game.ui.GameFrame;
import game.ui.Stage4;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Boss1 extends Enemy {
	/**
	 * Boss1
	 */
	public static BufferedImage img = FileTool.gi().loadImg("miniBoss1.gif");

	public int fireModel;
	public int bullteTypeInFire2;
	public int x_direction;
	public long time4;

	public Boss1(Double x, Double y, Long fireDelay, Integer mode) {
		this.time1 = 0;
		this.time2 = 0;
		this.time3 = 0;
		this.time4 = 0;
		this.mode = mode;
		this.x = x;
		this.y = y;
		this.hp = 400;
		this.radius = 54;
		this.fireDelay = fireDelay;
		this.x_speed = 1;
		this.y_speed = 2;
		this.fireModel = 1;
		this.bullteTypeInFire2 = 1;
		this.x_direction = 1;
	}

	@Override
	public void render(Graphics2D g2) {
		g2.drawImage(img, (int) x, (int) y - 20, null);
	}

	@Override
	public void update(long l) {
		if (this.hp > 0) {
			time1 += l;

			if (this.x < 100 | this.x > 190) {
				this.x_direction = -this.x_direction;
			}
			x = x + this.x_speed * this.x_direction * l / 15;

			if (this.y > 80) {
				if (this.time1 - this.time4 > 4000) {
					this.fireModel = -this.fireModel;
					this.time4 = this.time1;
				}

				if (this.fireModel == 1) {
					if (time1 - time2 > fireDelay) {
						this.fire1();
						this.time2 = this.time1;
					}
				} else if (this.fireModel == -1) {
					if (time1 - time3 > fireDelay) {
						this.fire2();
						this.bullteTypeInFire2 = -this.bullteTypeInFire2;
						this.time3 = this.time1;
					}
				}

			} else {
				y = y + this.y_speed * l / 15;
			}

		} else {
			this.die();
			this.valid = false;
		}
	}

	public void fire1() {
		((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
				.add(new Bullet_Red(this.x - 2, this.y + 85, this.aim1()));
		((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
				.add(new Bullet_Red(this.x + 13, this.y + 85, this.aim1()));
		((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
				.add(new Bullet_Red(this.x + 80, this.y + 85, this.aim2()));
		((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
				.add(new Bullet_Red(this.x + 95, this.y + 85, this.aim2()));
	}

	public void fire2() {
		if (this.bullteTypeInFire2 == 1) {
			((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
					.add(new Bullet_Green(this.x + 15, this.y + 40, 75));
			((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
					.add(new Bullet_Green(this.x + 15, this.y + 40, 90));
			((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
					.add(new Bullet_Green(this.x + 15, this.y + 40, 105));

			((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
					.add(new Bullet_Green(this.x + 95, this.y + 40, 75));
			((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
					.add(new Bullet_Green(this.x + 95, this.y + 40, 90));
			((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
					.add(new Bullet_Green(this.x + 95, this.y + 40, 105));
		} else {
			if (this.bullteTypeInFire2 == -1) {
				((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
						.add(new Bullet_Red(this.x + 15, this.y + 40, 75));
				((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
						.add(new Bullet_Red(this.x + 15, this.y + 40, 90));
				((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
						.add(new Bullet_Red(this.x + 15, this.y + 40, 105));

				((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
						.add(new Bullet_Red(this.x + 95, this.y + 40, 75));
				((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
						.add(new Bullet_Red(this.x + 95, this.y + 40, 90));
				((Stage4) GameFrame.gi().getCurrentCanvas()).bullets
						.add(new Bullet_Red(this.x + 95, this.y + 40, 105));
			}
		}
	}

	public void die() {
		Player.gi().score += 150;

		ArrayList<Canvas> c = ((Stage4) GameFrame.gi().getCurrentCanvas()).comps;

		c.add(new Explosion(this.x, this.y));

		c.add(new Collection(this.x, this.y));
		c.add(new Collection(this.x, this.y + 15));
		c.add(new Collection(this.x, this.y + 30));
		c.add(new Collection(this.x, this.y + 45));
		c.add(new Collection(this.x, this.y + 60));
		c.add(new Collection(this.x, this.y + 75));
		c.add(new Collection(this.x, this.y + 90));
		c.add(new Collection(this.x, this.y + 105));
		c.add(new Collection(this.x, this.y + 120));
		c.add(new Collection(this.x, this.y + 135));

		c.add(new Collection(this.x + 20, this.y));
		c.add(new Collection(this.x + 20, this.y + 15));
		c.add(new Collection(this.x + 20, this.y + 30));
		c.add(new Collection(this.x + 20, this.y + 45));
		c.add(new Collection(this.x + 20, this.y + 60));
		c.add(new Collection(this.x + 20, this.y + 75));
		c.add(new Collection(this.x + 20, this.y + 90));
		c.add(new Collection(this.x + 20, this.y + 105));
		c.add(new Collection(this.x + 20, this.y + 120));
		c.add(new Collection(this.x + 20, this.y + 135));

		SoundEngine.gi().playExplotion2();
	}

	public void beaten() {
		this.hp = this.hp - 1;
		SoundEngine.gi().playHitEnemy();
	}

	public double aim1() {
		double offX = Player.gi().x + Player.gi().radius - this.x - 5;
		double offY = Player.gi().y + Player.gi().radius - this.y - 85;
		double tan = offY / offX;
		double angle = Math.toDegrees(Math.atan(tan));
		if (offY > 0) {
			if (offX > 0) {
				return angle;
			} else {
				return angle + 180;
			}
		} else if (offX > 0) {
			return 360 + angle;
		} else {
			return angle + 180;
		}

	}

	public double aim2() {
		double offX = Player.gi().x + Player.gi().radius - this.x - 87;
		double offY = Player.gi().y + Player.gi().radius - this.y - 85;
		double tan = offY / offX;
		double angle = Math.toDegrees(Math.atan(tan));
		if (offY > 0) {
			if (offX > 0) {
				return angle;
			} else {
				return angle + 180;
			}
		} else if (offX > 0) {
			return 360 + angle;
		} else {
			return angle + 180;
		}

	}

}
